package demo.cypher;

import javax.crypto.*;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.io.*;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;

/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 14.04.11
 * Time: 23:31
 * To change this template use File | Settings | File Templates.
 */
public class FileEncrypter {
    byte[] buf = new byte[1024];
    Cipher ecipher;
    Cipher dcipher;

    public FileEncrypter(String passPhrase) {

        // 8-bytes Salt
        byte[] salt = {
                (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
                (byte) 0x56, (byte) 0x34, (byte) 0xE3, (byte) 0x03
        };

        // Iteration count
        int iterationCount = 19;

        try {

            KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
            SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);

            ecipher = Cipher.getInstance(key.getAlgorithm());
            dcipher = Cipher.getInstance(key.getAlgorithm());

            // Prepare the parameters to the cipthers
            AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

            ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
            dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

        } catch (InvalidAlgorithmParameterException e) {
//            System.out.println("EXCEPTION: InvalidAlgorithmParameterException");
        } catch (InvalidKeySpecException e) {
//            System.out.println("EXCEPTION: InvalidKeySpecException");
        } catch (NoSuchPaddingException e) {
//            System.out.println("EXCEPTION: NoSuchPaddingException");
        } catch (NoSuchAlgorithmException e) {
//            System.out.println("EXCEPTION: NoSuchAlgorithmException");
        } catch (InvalidKeyException e) {
//            System.out.println("EXCEPTION: InvalidKeyException");
        }
    }

    public void encrypt(InputStream in, OutputStream out) {
        try {
            // Bytes written to out will be encrypted
            out = new CipherOutputStream(out, ecipher);

            // Read in the cleartext bytes and write to out to encrypt
            int numRead = 0;
            while ((numRead = in.read(buf)) >= 0) {
                out.write(buf, 0, numRead);
            }
            in.close();
            out.close();
        } catch (java.io.IOException e) {
        }
    }

    public void decrypt(InputStream in, OutputStream out) {
        try {
            // Bytes read from in will be decrypted
            in = new CipherInputStream(in, dcipher);

            // Read in the decrypted bytes and write the cleartext to out
            int numRead = 0;
            while ((numRead = in.read(buf)) >= 0) {
                out.write(buf, 0, numRead);
            }
            in.close();
            out.close();
        } catch (java.io.IOException e) {
        }
    }

    public InputStream getDecryptInputStreamReader(InputStream in){
        InputStream inDec = new CipherInputStream(in, dcipher);
        return inDec;
    }


//    public static void main(String[] args) throws FileNotFoundException {
//        //        encryption phase
//        String keyPhase = "javasun";
//        FileEncrypter fe = new FileEncrypter(keyPhase);
//
////        fe.encrypt(new FileInputStream("c:\\myFile.txt"), new FileOutputStream("c:\\myFileEN.txt"));
//
//        fe.decrypt(new FileInputStream("D:/Others/2011-04-15_test_fs1.sql"), new FileOutputStream("D:/Others/2011-04-15_test_fs1.sql"));
//
//    }
}
